package org.feng.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.feng.entity.SysGenerate;
import org.feng.entity.SysTable;
import org.feng.condition.Condition;
import org.feng.annotation.AccessLog;
import org.feng.base.entity.Result;
import org.feng.support.Generate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.feng.service.ISysGenerateService;
import org.feng.base.controller.BaseController;

import jakarta.annotation.Resource;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Arrays;

/**
 * 代码生成表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@RestController
@RequestMapping("/generate/sysGenerate")
@Tag(name = "代码生成")
public class SysGenerateController extends BaseController {

    @Resource
    private ISysGenerateService sysGenerateService;
    @Resource
    private Generate generate;

    @GetMapping("/build")
    @AccessLog("代码生成")
    @Operation(summary = "代码生成")
    public Result<Boolean> build(@Validated @NotBlank(message = "参数错误") String id) {
        SysGenerate sysGenerate = sysGenerateService.getById(id);
        if (StrUtil.isEmpty(sysGenerate.getModule())) {
            return Result.error("模块不能为空");
        }
        if (StrUtil.isEmpty(sysGenerate.getPackageName())) {
            return Result.error("包名不能为空");
        }
        if (sysGenerate.getParent() == null) {
            return Result.error("请选择继承");
        }
        generate.execute(sysGenerate);
        return Result.success();
    }

    @GetMapping("/tablePage")
    @Operation(summary = "获取分页数据库表")
    public Result<IPage<SysTable>> tablePage(SysTable sysTable) {
        return Result.success(sysGenerateService.tablePage(Condition.getPage(), sysTable));
    }

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public Result<IPage<SysGenerate>> pageList(SysGenerate sysGenerate) {
        return Result.success(sysGenerateService.page(Condition.getPage(), Wrappers.lambdaQuery(sysGenerate)));
    }

    @GetMapping("/list")
    @Operation(summary = "列表")
    public Result<List<SysGenerate>> list(SysGenerate sysGenerate) {
        return Result.success(sysGenerateService.list(Wrappers.lambdaQuery(sysGenerate)));
    }

    @PostMapping("/save")
    @AccessLog("新增代码生成表")
    @Operation(summary = "保存")
    public Result<Boolean> save(@RequestBody SysGenerate sysGenerate) {
        return Result.status(sysGenerateService.save(sysGenerate));
    }

    @PostMapping("/update")
    @AccessLog("更新代码生成表")
    @Operation(summary = "更新")
    public Result<Boolean> update(@RequestBody SysGenerate sysGenerate) {
        return Result.status(sysGenerateService.updateById(sysGenerate));
    }

    @GetMapping("/remove")
    @AccessLog("删除代码生成表")
    @Operation(summary = "删除")
    public Result<Boolean> remove(@RequestParam("ids") String ids) {
        return Result.status(sysGenerateService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }
}
